package gui.mainWindow;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import questions.Entry;
import questions.Questions;
import net.miginfocom.swing.MigLayout;
import java.awt.Component;
import javax.swing.Box;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

@SuppressWarnings("serial")
public class QuestionsPanel extends JPanel implements ActionListener{

	/**
	 * Create the panel.
	 */
		private JLabel lblSection = new JLabel("SectionLabel");
		private JLabel lblType = new JLabel("typeLabel");
		private JLabel lblQuestion = new JLabel("testo che devo visualizzare prima");
		private JLabel lblPagina = new JLabel("Pagina:");
		private JLabel lblPageNum = new JLabel("34");
		private JLabel lblDate = new JLabel("Last: ");
		private JLabel lblDateNum = new JLabel("15/4/91");
		private JLabel lblRate = new JLabel("Rate: ");
		private JButton btnQuit = new JButton("Quit & Save");
		private JButton btnNext = new JButton("Next");
		
		private Questions questions;
		private MainWindow mainW;
		private Entry entry;
		
		private static SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yy  k:m", Locale.ITALIAN);
		private final JSpinner spinner = new JSpinner();
		
		
		
		public QuestionsPanel(Questions q, MainWindow m) {
			questions = q;
			mainW = m;
			
			setLayout(new MigLayout("", "[grow][40:n][][][][][grow][][][][][40:n][grow]", "[grow][40:n][][][20:n,grow][][20,grow][][grow][][40:n][grow]"));
			lblSection.setFont(new Font("Dialog", Font.BOLD, 15));
						
			add(lblSection, "cell 2 2 9 1,alignx center");
			lblType.setFont(new Font("Dialog", Font.PLAIN, 12));
			
			add(lblType, "cell 2 3 9 1,alignx center");
			lblQuestion.setFont(new Font("Dialog", Font.ITALIC, 16));
			
			add(lblQuestion, "cell 2 5 9 1,alignx center");
			lblPagina.setFont(new Font("Dialog", Font.PLAIN, 12));
			
			add(lblPagina, "cell 2 7");
			
			add(lblPageNum, "cell 3 7");
			lblDate.setFont(new Font("Dialog", Font.PLAIN, 12));
			
			add(lblDate, "cell 7 7");
			
			add(lblDateNum, "cell 8 7");
			lblRate.setFont(new Font("Dialog", Font.PLAIN, 12));
			
			add(lblRate, "cell 9 7");
			
			spinner.setModel(new SpinnerNumberModel(1, 1, 5, 1));
			
			add(spinner, "cell 10 7");
			
			JPanel panel = new JPanel();
			add(panel, "cell 2 9 9 1,grow");
			
			btnQuit.setActionCommand("quit");
			btnQuit.addActionListener(this);
			btnNext.setActionCommand("next");
			btnNext.addActionListener(this);
			
			Component horizontalGlue = Box.createHorizontalGlue();
			panel.add(horizontalGlue);
			
			panel.add(btnQuit);
			panel.add(btnNext);
			
			Component horizontalGlue_1 = Box.createHorizontalGlue();
			panel.add(horizontalGlue_1);
			
		
		}
		
		public void showQuestion(Entry e){
			entry = e;
			
			
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					
					lblSection.setText(entry.getSection());
					lblType.setText(entry.getType());
					lblQuestion.setText(entry.getQuestion());
					lblPageNum.setText(Integer.toString(entry.getPage()));
					
					if(entry.getDate() == 0)
						lblDateNum.setText("never");
					else
						lblDateNum.setText(dateFormatter.format(new Date(entry.getDate())));
					
					//lblRateNum.setText(Integer.toString(entry.getPriority()));
					spinner.setValue(entry.getPriority());
					mainW.reconfFrame();
				}
			});
			
		}

		
		public void actionPerformed(ActionEvent arg) {
			if(arg.getActionCommand().equals("quit")){
				Thread t = new Thread(new Runnable() {
					public void run() {
						try {
							questions.saveProgress();
							entry = null;
						} catch (IOException e) {
							JOptionPane.showMessageDialog(null, "Errore nel salvataggio", "Errore", JOptionPane.ERROR_MESSAGE, null);
							e.printStackTrace();
							System.exit(1);
						}
						mainW.showMenuPanel();
					}
				});
				t.start();			
			}else if(arg.getActionCommand().equals("next")){
				Thread t = new Thread(new Runnable() {				
					public void run() {
						updateEntry();
						questions.reinsertEntry(entry);
						showQuestion(questions.getEntry());
					}
				});
				t.start();
			}
		}
		
		private void updateEntry(){
			entry.setDate(System.currentTimeMillis());
			
			entry.setPriority((Integer) spinner.getValue());
		}
}


